const express = require('express'); 
const router = express.Router();
const { Menu } = require('../../models/menu');

// 增加角色
router.post('/add', (req, res) => {
	console.log(req.body,"888888888888888");
	const data=req.body
	new Menu(data).save().then(result => {
		res.json({
			code:200,
			data: result,
			msg: '添加成功'
		});
	});
});
// 查询
router.get('/search', async (req, res) => {
	const data = await Menu.find();
	res.json({
		code:200,
		msg: '菜单查询成功',
		data: data,
	});
});

// 删除
router.post("/delete/:id",(req, res) => {
	Menu.findByIdAndDelete({_id:req.params.id}).then(profile=>{
		 console.log(profile);
		res.json({
			code:200,
			data: profile,
			msg: '删除成功'
		});
	}).catch(err=>{
		res.json({
			data: err,
			msg: '删除失败'
		});
	})
})
// 修改
router.post("/edit/:id",(req, res) => {
	let menuItem=req.body
	Menu.findByIdAndUpdate(
			{_id:req.params.id},
			{$set:menuItem},
			{new:true}
	).then(profile=>{
		res.json({
			code:200,
			msg: '菜单修改成功',
			data: profile,
		});
	}).catch(err=>res.json(err))
})


module.exports = router;
